package com.kanshu.ksgb.fastread.doudou.module.makemoney.presenter;

import android.util.Log;

import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.BaseMvpPresenter;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.PageRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.bean.ApprenticeBean;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.bean.AutoReceiveBean;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.bean.EverydayBean;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.bean.InvitationBean;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.bean.WeeklyBean;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.retrofit.MakeMoneyService;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.retrofit.ReceiveParams;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.Subject;
import okhttp3.ResponseBody;

/**
 * Created by wcy on 18/5/29.
 */

public class MakeMoneyPresenter extends BaseMvpPresenter {
    MakeMoneyService mMoneyService;
    IGenrialMvpView<BaseResult<EverydayBean>> mEverydayView;
    IGenrialMvpView<BaseResult<WeeklyBean>> mWeeklyView;
    public void setEverydayView(IGenrialMvpView v) {
        mEverydayView = v;
    }
    public void setWeeklyView(IGenrialMvpView v) {
        mWeeklyView = v;
    }
    public MakeMoneyPresenter(Subject<Integer> subject) {
        super(subject);
        RetrofitHelper helper = RetrofitHelper.getInstance();
        mMoneyService = helper.createService(MakeMoneyService.class);
    }

    public void uploadReadTime(int min, INetCommCallback<Boolean> callback) {
        Map<String, String> map = new HashMap<>();
        map.put("minute", min + "");
        mMoneyService.uploadReadTime(map).compose(asyncRequest()).subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
            @Override
            public void onNext(ResponseBody responseBody) {
                String s = null;
                try {
                    s = responseBody.string();
                    JSONObject object = new JSONObject(s);
                    JSONObject result = object.getJSONObject("result");
                    JSONObject status = result.getJSONObject("status");
                    int code = status.getInt("code");
                    String str = status.getString("msg");
                    Log.d("wcy", "delrecentbook:" + s);
                    if (code == 0) {
                        callback.onResponse(true);
                    } else {
                        callback.onError(code, str);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
            }
            @Override
            public void onComplete() {}
        });
    }


    public void getEverydayInfo() {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mEverydayView != null) {
                mEverydayView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mMoneyService.getEverydayInfo().compose(asyncRequest()).subscribe(new BaseObserver<EverydayBean>() {
            @Override
            public void onResponse(BaseResult<EverydayBean> listBaseResult, EverydayBean selectedBeans, Disposable disposable) {
                if (mEverydayView != null && BaseResult.isNotNull(listBaseResult)) {
                    mEverydayView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mEverydayView != null) {
                    mEverydayView.showError(code, errDesc);
                }
            }
        });
    }

    public void getWeeklyInfo() {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (mWeeklyView != null) {
                mWeeklyView.showError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mMoneyService.getWeeklyInfo().compose(asyncRequest()).subscribe(new BaseObserver<WeeklyBean>() {
            @Override
            public void onResponse(BaseResult<WeeklyBean> listBaseResult, WeeklyBean selectedBeans, Disposable disposable) {
                if (mWeeklyView != null && BaseResult.isNotNull(listBaseResult)) {
                    mWeeklyView.showContent(listBaseResult);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (mWeeklyView != null) {
                    mWeeklyView.showError(code, errDesc);
                }
            }
        });
    }

    @Override
    public void detachView() {
        super.detachView();
        mWeeklyView = null;
        mEverydayView = null;
    }

    public void getApprenticeList(PageRequestParams params, INetCommCallback<List<ApprenticeBean>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mMoneyService.getApprenticeList(params).compose(asyncRequest()).subscribe(new BaseObserver<List<ApprenticeBean>>() {
            @Override
            public void onResponse(BaseResult<List<ApprenticeBean>> listBaseResult, List<ApprenticeBean> selectedBeans, Disposable disposable) {
                if (callback != null && BaseResult.isNotNull(listBaseResult)) {
                    callback.onResponse(selectedBeans);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    public void getInvitationInfo( INetCommCallback<InvitationBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
                return;
            }
        }
        mMoneyService.getInvitationInfo().compose(asyncRequest()).subscribe(new BaseObserver<InvitationBean>() {
            @Override
            public void onResponse(BaseResult<InvitationBean> listBaseResult, InvitationBean selectedBeans, Disposable disposable) {
                if (callback != null && BaseResult.isNotNull(listBaseResult)) {
                    callback.onResponse(selectedBeans);
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    public void receiveTask(ReceiveParams params, INetCommCallback<Boolean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
            }
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("receive_type", params.receive_type);
        mMoneyService.receiveTask(map).compose(asyncRequest()).subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
            @Override
            public void onNext(ResponseBody responseBody) {
                String s = null;
                try {
                    s = responseBody.string();
                    JSONObject object = new JSONObject(s);
                    JSONObject result = object.getJSONObject("result");
                    JSONObject status = result.getJSONObject("status");
                    int code = status.getInt("code");
                    String str = status.getString("msg");
                    Log.d("wcy", "delrecentbook:" + s);
                    if (code == 0) {
                        callback.onResponse(true);
                    } else {
                        callback.onError(code, str);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
            }
            @Override
            public void onComplete() {}
        });
    }

    public void getNotifyList(INetCommCallback<List<String>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
            }
            return;
        }
        mMoneyService.getNotifyList().compose(asyncRequest()).subscribe(new BaseObserver<List<String>>() {
            @Override
            public void onResponse(BaseResult<List<String>> listBaseResult, List<String> strings, Disposable disposable) {
                if (callback != null && BaseResult.isNotNull(listBaseResult)) {
                    callback.onResponse(strings);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    public void autoReceive(INetCommCallback<AutoReceiveBean> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null) {
                callback.onError(Constants.ErrCode.NetErr, "no net");
            }
            return;
        }
        Map<String, String> map = new HashMap<>();
        mMoneyService.getAutoReceiveTask(map).compose(asyncRequest()).subscribe(new BaseObserver<AutoReceiveBean>() {
            @Override
            public void onResponse(BaseResult<AutoReceiveBean> listBaseResult, AutoReceiveBean strings, Disposable disposable) {
                if (callback != null && BaseResult.isNotNull(listBaseResult)) {
                    callback.onResponse(strings);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

}
